package org.jeecg.modules.tankerRegistration.util;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.RestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Component
public class TemplateMessageUtil {

    private static final Logger log = LoggerFactory.getLogger(TemplateMessageUtil.class);
    private final RedisUtil redisUtil;

    //太维科技公众号
//	 private static final String appid = "wx35cfb8619f70829d";
//	 private static final String secret = "ad9fa5ffe510db2f88d3164c119444c1";
    //镇江中储粮公众号
    private static final String APPID = "wx026cd17aaa218445";
    private static final String SECRET = "c30815b8329dd4e79d0db6dc0f1613c8";

    private static final String WX_TOKEN = "zclzj_wx_token";

    private static final String[] transport = {"", "提货", "送货", "液袋"};

    public TemplateMessageUtil(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    /**
     * 推送车辆三载审核结果
     * @param touser
     * @param plateNo
     * @param transportStatus
     * @param reason
     * @param date
     */
    public void carRegister(String touser, String plateNo, Integer transportStatus, String reason, Date date) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        JSONObject jsonObj=new JSONObject();

        JSONObject data=new JSONObject();

        JSONObject car_number3=new JSONObject();
        car_number3.put("value",plateNo);

        JSONObject short_thing7=new JSONObject();
        short_thing7.put("value",transport[transportStatus]);

        JSONObject thing11=new JSONObject();
        thing11.put("value", reason);

        JSONObject time1=new JSONObject();
        time1.put("value", DateUtils.formatTime(date));

        data.put("car_number3",car_number3);
        data.put("short_thing7",short_thing7);
        data.put("thing11",thing11);
        data.put("time1",time1);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id", "_aR16fhVSQpfifXAVoXMLwRTKW0YWmPSxt5r-3aJDdM");//模板ID
        jsonObj.put("url", "http://zclzjdj.ygycxt.com.cn/?tanker=2");//模板跳转链接
        jsonObj.put("data",data);//模板数据

        JSONObject result= RestUtil.post(path,jsonObj);
        if(result.getInteger("errcode")!=0){
            log.info("模板消息发送失败: {}", result);
        }
    }

    /**
     * 推送船三载审核结果
     * @param touser
     * @param plateNo
     * @param transportStatus
     * @param reason
     * @param date
     */
    public void shipRegister(String touser, String plateNo, Integer transportStatus, String reason, Date date) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        JSONObject jsonObj=new JSONObject();

        JSONObject data=new JSONObject();

        JSONObject thing40=new JSONObject();
        thing40.put("value",plateNo);

        JSONObject short_thing7=new JSONObject();
        short_thing7.put("value",transport[transportStatus]);

        JSONObject thing11=new JSONObject();
        thing11.put("value", reason);

        JSONObject time1=new JSONObject();
        time1.put("value", DateUtils.formatTime(date));

        data.put("thing40",thing40);
        data.put("short_thing7",short_thing7);
        data.put("thing11",thing11);
        data.put("time1",time1);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id", "_aR16fhVSQpfifXAVoXML5dCdqmG6daqjZr_kcuc2xU");//模板ID
        jsonObj.put("url", "http://zclzjdj.ygycxt.com.cn/?tanker=2");//模板跳转链接
        jsonObj.put("data",data);//模板数据

        JSONObject result= RestUtil.post(path,jsonObj);
        if(result.getInteger("errcode")!=0){
            log.info("模板消息发送失败: {}", result);
        }
    }

    /**
     * 推送员工当前车辆流程
     * @param touser
     * @param plateNo
     */
    public void sendCarOA(String touser, String plateNo, String productName, String driverName, String phone) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        String template_id = "umi9VkNvSigOdgb222TnSSjXEnNiZbBrRl7EYHewPzk";
        String url = "http://zclzjdj.ygycxt.com.cn/?tanker=1";
        JSONObject jsonObj=new JSONObject();

        JSONObject data=new JSONObject();

        JSONObject car_number3=new JSONObject();
        car_number3.put("value",plateNo);

        JSONObject thing8=new JSONObject();
        thing8.put("value", productName);

        JSONObject thing2=new JSONObject();
        thing2.put("value", driverName);

        JSONObject phone_number15=new JSONObject();
        phone_number15.put("value", phone);

        data.put("car_number6",car_number3);
        data.put("thing8",thing8);
        data.put("thing2",thing2);
        data.put("phone_number15",phone_number15);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id",template_id);//模板ID
        jsonObj.put("url", url);//模板跳转链接
        jsonObj.put("data",data);//模板数据

        JSONObject result= RestUtil.post(path,jsonObj);
        if(result.getInteger("errcode")!=0){
            log.info("模板消息发送失败: {}", result);
        }
    }

    /**
     * 推送员工当前船流程
     * @param touser
     * @param plateNo
     */
    public void sendShipOA(String touser, String plateNo, String productName, String driverName, String phone) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        String template_id = "umi9VkNvSigOdgb222TnSSSY8OBsBh3ZPBXi6El08QQ";
        String url = "http://zclzjdj.ygycxt.com.cn/?tanker=1";
        JSONObject jsonObj=new JSONObject();

        JSONObject data=new JSONObject();

        JSONObject thing20=new JSONObject();
        thing20.put("value",plateNo);

        JSONObject thing8=new JSONObject();
        thing8.put("value", productName);

        JSONObject thing2=new JSONObject();
        thing2.put("value", driverName);

        JSONObject phone_number15=new JSONObject();
        phone_number15.put("value", phone);

        data.put("thing20",thing20);
        data.put("thing8",thing8);
        data.put("thing2",thing2);
        data.put("phone_number15",phone_number15);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id",template_id);//模板ID
        jsonObj.put("url", url);//模板跳转链接
        jsonObj.put("data",data);//模板数据

        JSONObject result= RestUtil.post(path,jsonObj);
        if(result.getInteger("errcode")!=0){
            log.info("模板消息发送失败: {}", result);
        }
    }

    /**
     * 提醒司机上传铅封
     * @param touser
     * @param plateNo
     * @param driverName
     */
    public void sendCarLeadSealing(String touser, String plateNo, String driverName, String message) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        JSONObject jsonObj=new JSONObject();

        JSONObject data=new JSONObject();

        JSONObject car_number6=new JSONObject();
        car_number6.put("value",plateNo);

        JSONObject thing7=new JSONObject();
        thing7.put("value", driverName);

        JSONObject phrase3=new JSONObject();
        phrase3.put("value", message);

        JSONObject time4=new JSONObject();
        time4.put("value", DateUtils.formatDateTime());

        data.put("car_number6",car_number6);
        data.put("thing7",thing7);
        data.put("phrase3",phrase3);
        data.put("time4",time4);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id", "_2fv70JIIRjmXjGBCT1F-P2rYVz16bMdiev0l3QhqUE");//模板ID
        jsonObj.put("url", "http://zclzjdj.ygycxt.com.cn/?tanker=3");//模板跳转链接
        jsonObj.put("data",data);//模板数据

        JSONObject result= RestUtil.post(path,jsonObj);
        if(result.getInteger("errcode")!=0){
            log.info("模板消息发送失败: {}", result);
        }
    }

    /**
     * 提醒船上传铅封
     * @param touser
     * @param plateNo
     * @param driverName
     */
    public void sendShipLeadSealing(String touser, String plateNo, String driverName, String message) {
        String path="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getToken();
        JSONObject jsonObj=new JSONObject();

        JSONObject data=new JSONObject();

        JSONObject thing8=new JSONObject();
        thing8.put("value",plateNo);

        JSONObject thing9=new JSONObject();
        thing9.put("value", driverName);

        JSONObject phrase3=new JSONObject();
        phrase3.put("value", message);

        JSONObject time4=new JSONObject();
        time4.put("value", DateUtils.formatDateTime());

        data.put("thing8",thing8);
        data.put("thing9",thing9);
        data.put("phrase3",phrase3);
        data.put("time4",time4);

        jsonObj.put("touser",touser);//接收者openid
        jsonObj.put("template_id", "_2fv70JIIRjmXjGBCT1F-PE300VsMV0XRxLLZquez0I");//模板ID
        jsonObj.put("url", "http://zclzjdj.ygycxt.com.cn/?tanker=3");//模板跳转链接
        jsonObj.put("data",data);//模板数据

        JSONObject result= RestUtil.post(path,jsonObj);
        if(result.getInteger("errcode")!=0){
            log.info("模板消息发送失败: {}", result);
        }
    }

    public String getToken() {
        if(redisUtil.hasKey(WX_TOKEN)){
            Object token=redisUtil.get(WX_TOKEN);
            if(token!=null&& !StringUtils.isEmpty(token.toString())){
                return token.toString();
            }else{
                redisUtil.set(WX_TOKEN,getWXToken(),7000);
            }
        }else{
            redisUtil.set(WX_TOKEN,getWXToken(),7000);
        }
        return redisUtil.get(WX_TOKEN).toString();
    }

    public String getWXToken() {
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ APPID + "&secret=" + SECRET;
        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject(url, String.class);
        JSONObject jsonObj = JSONObject.parseObject(forObject);
        if(jsonObj.containsKey("access_token")){
            return jsonObj.getString("access_token");
        } else {
            log.info("获取token失败: {}", jsonObj);
        }
        return "";
    }
}
